﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Collections;
using Aulac.Client.Data.XmlQuery;
using Aulac.Global.Xml;
using Aulac.Global.Common;
using Aulac.Rsc.Bss.Tags;
using Aulac.Rsc.Bss;

namespace Aulac.Client.Controls.Forms.Bss
{
    public partial class FormEmsListItemsData : Aulac.Client.Controls.Forms.Bss.FormEmsDataListBase, IFormListData
    {
        private  ClientExecXmlQueryDataCatalog mQueryXml = null;
        public FormEmsListItemsData()
        {
            InitializeComponent();
        }
        #region Override Methods
        public override void OnFormLoad()
        {
            this.AulacToolbarNavCtrl.ShowControl(false, this.AulacToolbarNavCtrl.NextButton, this.AulacToolbarNavCtrl.BackButton);
            base.OnFormLoad();
        }
       
        protected override void OnRefreshList()
        {
            base.OnRefreshList();
            grdList.ClearDataContainer();
            CreateXmlQuery();
            Aulac.Client.Data.Helper.DataHelper.GetDataFromServerToControl(this.grdList, mQueryXml.XmlQueryData.XmlQueryDoc);

        }
        
        protected override void OnSetStateControls(Aulac.Global.Enums.FormModes pFormMode)
        {
            base.OnSetStateControls(pFormMode);
            if (pFormMode == Aulac.Global.Enums.FormModes.EditMode || pFormMode == Aulac.Global.Enums.FormModes.NewMode)
                AulacToolbarNavCtrl.FormMode = Aulac.Global.Enums.FormModes.LockMode;
            else AulacToolbarNavCtrl.FormMode = pFormMode;
                
        }

        protected virtual void OnAddParamsQuery()
        {

        }

        protected virtual void OnAddColumnsTableQuery()
        {

        }

        protected virtual void OnSetDataSource(out object pDataSource, out string pTableName)
        {
            pDataSource = null;
            pTableName = "";
        }

        protected virtual string OnCreateQueryString()
        {
            return "";
        }

        protected virtual string OnCreateFilterString()
        {
            return "";
        }
        #endregion
        #region Public Methods
        public void AddParamQuery(string pFieldName, string pParamName,  params object[] pArrValue)
        {
            XmlElement eLines = mQueryXml.XmlQueryData.GetVchLinesElm();
            mQueryXml.XmlQueryData.AddValueParamFilterItem(eLines, pFieldName, pParamName, pArrValue);
        }
        
        public void AddParamQuery(string pFieldName, string pParamName, System.Data.DbType  pDataType, params object[] pArrValue)
        {
            XmlElement eLines = mQueryXml.XmlQueryData.GetVchLinesElm();
            mQueryXml.XmlQueryData.AddValueParamFilterItem(eLines, pFieldName, pParamName, pDataType, pArrValue);
        }
        
        public void AddParamQuery(string pFieldName, string pParamName, System.Data.DbType pDataType, IList pArrValue)
        {
            XmlElement eLines = mQueryXml.XmlQueryData.GetVchLinesElm();
            Type t = pArrValue.GetType();
            object[] arr = new object[pArrValue.Count];
            int i = 0;
            foreach (object o in pArrValue)
                arr[i++] = o;
            mQueryXml.XmlQueryData.AddValueParamFilterItem(eLines, pFieldName, pParamName, pDataType, (object[])arr);
        }
        
        public void AddColumnQuery(string pFieldName, System.Data.DbType pDataType, string pFieldCaption)
        {

            XmlElement elm = mQueryXml.XmlQueryData.GetVchLinesElm();
            mQueryXml.XmlQueryData.AddFieldResultItem( elm, pFieldName, pDataType, pFieldCaption);
        }

        public void AddColumnQuery(string pFieldName)
        {
            XmlElement elm = mQueryXml.XmlQueryData.GetVchLinesElm();
            mQueryXml.XmlQueryData.AddFieldResultItem( elm, pFieldName, DbType.String, "");
        }
        
        public void AddListFieldQuery(params string[] pArrFields)
        {
            XmlElement elm = mQueryXml.XmlQueryData.GetVchLinesElm();
            mQueryXml.XmlQueryData.AddFieldInfoNameRange(elm, pArrFields);
        }
        
        public void AddColumnQuery(string pFieldName, DbType pDataType)
        {

            XmlElement elm = mQueryXml.XmlQueryData.GetVchLinesElm();
            mQueryXml.XmlQueryData.AddFieldResultItem( elm, pFieldName, pDataType, "");
        }
       
        public void AddColumnQuery(string pFieldName, string pFieldCaption)
        {

            XmlElement elm = mQueryXml.XmlQueryData.GetVchLinesElm();
            mQueryXml.XmlQueryData.AddFieldResultItem( elm, pFieldName, DbType.String, pFieldCaption);
        }
        #endregion
       
        #region Properties
        private string mStrVerb = Aulac.Verb.AulacVerb.Catalog.GetListDataItems;
        public string StrVerb
        {
            set { mStrVerb = value; }
            get {return mStrVerb;}
        }
        private string mStrNoun = Aulac.Verb.AulacVerb.Catalog.Noun.AulacCatalog;//Aulac.Verb.AulacVerb.Catalog.Noun.AulacCatalog;

        public string StrNoun
        {
            set { mStrNoun = value; }
            get { return mStrNoun; }
        }
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public XmlQueryData XmlQueryData
        {
            get
            {
                return mQueryXml.XmlQueryData;
            }
        }

        /// <summary>
        /// Lấy tên danh mục.
        /// </summary>
        /// <returns>Tên danh mục</returns>
        public string GetTileDmTypesName(Dm_TbName DmTypesName)
        {
            ClientExecXmlQueryData query = new ClientExecXmlQueryData();
            query.SetRequestInfo(Verb.AulacVerb.DataQuery.QueryData, Verb.AulacVerb.DataQuery.Noun.AulacDataQuery);
            query.SetVchHeaderElm();
            string strQuery = string.Format("SELECT {0} ", tagItems.DmTypesName.DmName);
            strQuery += string.Format(" FROM {0} ", TableNames.Dm_DmTypesName);
            strQuery += string.Format(" WHERE {0} = @{0}", tagItems.DmTypesName.DmID);
            query.ObjQueryVchHeader.SqlQueryString = strQuery;
            query.ObjQueryVchHeader.AddValueParamFilterItem(tagItems.DmTypesName.DmID, tagItems.DmTypesName.DmID, DmTypesName);

            try
            {
                return XmlProcess.GetValueVchHeader(query.ExecureQueryData(), tagItems.DmTypesName.DmName);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }
        private string mQueryString = "";
        /// <summary>
        /// Chuổi truy vấn
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string QueryString
        {
            //get { return mQueryString; }
            set
            {
                mQueryString = value;
                XmlElement eLines = mQueryXml.XmlQueryData.GetVchLinesElm();
                mQueryXml.XmlQueryData.SetQueryString(eLines, value);
            }
        }

        #endregion
        private void CreateXmlQuery()
        {
            if (mQueryXml == null)
                mQueryXml = new ClientExecXmlQueryDataCatalog();

            mQueryXml.XmlQueryData.SetRequestInfo(StrVerb, StrNoun);
            mQueryXml.XmlQueryData.SetXmlDocTypeReturn(XmlDocTypes.XmlCatalog);
            XmlElement eLines = mQueryXml.XmlQueryData.SetVchLinesElm();
            mQueryXml.XmlQueryData.RemoveAllParamFilterItem(eLines);
            OnAddParamsQuery();
            mQueryXml.XmlQueryData.RemoveResultElm(eLines);
            object ds = null;
            string lTable = "";
            OnSetDataSource(out ds, out lTable);
            this.TableName = lTable;
            //grdList.DataSourceName = mTable;
            
            if(Aulac.Global.Common.ValueHelper.isEmpty(ds) == false)
            grdList.DataSource = ds;

            OnAddColumnsTableQuery();
            if (mQueryXml.XmlQueryData.GetListFieldNameResult(eLines).Count == 0)
            {
                
                if (mQueryXml.XmlQueryData.GetListFieldInfoName(eLines).Count == 0)
                {
                    DataTable tb = grdList.GetDataTableFromDataSource();
                    List<string> list = new List<string>();
                    foreach (DataColumn col in tb.Columns)
                        list.Add(col.ColumnName);
                    mQueryXml.XmlQueryData.AddFieldInfoNameRange(eLines, list.ToArray());
                }
            }
           
            
            string sqlQuery = OnCreateQueryString();
            if (Aulac.Global.Common.ValueHelper.isEmpty(sqlQuery) == false)
                mQueryXml.XmlQueryData.SetQueryString(eLines, sqlQuery);
            string sFilter = OnCreateFilterString();
            if (Aulac.Global.Common.ValueHelper.isEmpty(sFilter) == false)
                mQueryXml.XmlQueryData.SetFilterString(eLines, sFilter);
            mQueryXml.XmlQueryData.SetTableName(eLines, TableName);
        }

        #region IFormListData Members
        void IFormListData.AddItemToList(XmlElement eItem)
        {
            throw new NotImplementedException();
        }
        void IFormListData.BackVoucher()
        {
            throw new NotImplementedException();
        }
        void IFormListData.DelItemInList(int pRowViewIndex)
        {
            throw new NotImplementedException();
        }
        Aulac.Global.Enums.FormModes IFormListData.FormMode
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        object IFormListData.GetValueItem(int pRowViewIndex, string pFieldName)
        {
            throw new NotImplementedException();
        }
        object IFormListData.GetValuePrkID(int pRowViewIndex, string pFieldPrkID)
        {
            throw new NotImplementedException();
        }
        bool IFormListData.IsUpdateChanged
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        void IFormListData.NextVoucher()
        {
            throw new NotImplementedException();
        }
        int IFormListData.SelectedViewIndex
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        void IFormListData.SetBidingControl(params Aulac.Client.Global.Interface.IAulacDataControl[] pArrCtrs)
        {
            throw new NotImplementedException();
        }
        void IFormListData.SetItemToList(int pRowViewIndex, XmlElement eItem)
        {
            throw new NotImplementedException();
        }
        string IFormListData.TableName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        #endregion
    }
}
